const {hitsCount} = require('../../utils/cloudRequest')
const {article, food} = require('../../utils/config')
const {getHotList, searchResult} = require('../../utils/request')

Page({

    data: {
        hotList: [],
        collection: '',
        keyword: '',
        searchStatus: false,
        dataList: []
    },
    //搜索
    getSearchContent(event) {
        this.setData({
            keyword: event.detail.value,
            searchStatus: true
        });

        if (event.detail.value === '') return
        searchResult(this.data.collection, event.detail.value).then(res => {
            this.setData({
                dataList: res.data
            })
        })
    },
    //取消搜索
    clearKeyword() {
        this.setData({
            keyword: '',
            dataList: [],
            searchStatus: false
        })
    },
    //跳转文章播放页
    toArticleDetail(event) {
        const {id} = event.currentTarget.dataset
        hitsCount(this.data.collection, id).then()
        wx.navigateTo({
            url: `../articleDetail/index?id=${id}`
        })
    },
    onArticleDetail(event) {
        const id = event.detail
        hitsCount(this.data.collection, id).then()
        wx.navigateTo({
            url: `../articleDetail/index?id=${id}`
        })
    },
    //跳转食物详情信息页
    toFoodDetail(event) {
        const {id} = event.currentTarget.dataset
        hitsCount(this.data.collection, id).then()
        wx.navigateTo({
            url: `../foodDetail/index?id=${id}`
        })
    },
    onFoodDetail(event) {
        const id = event.detail
        hitsCount(this.data.collection, id).then()
        wx.navigateTo({
            url: `../foodDetail/index?id=${id}`
        })
    },
    onLoad(event) {
        const {
            param
        } = event;
        //传过来的参数为 0 搜索对象为视频数据库，否则为食物数据库
        param === '0' ?
            this.setData({
                collection: article
            }) :
            this.setData({
                collection: food
            })
        //获取热门
        getHotList(this.data.collection).then(res => {
            this.setData({
                hotList: res.data
            })
        })
    },
})